<?php

class Admin_Model_DbTable_Pointbassin extends Zend_Db_Table
{
    protected $_name = 'pointbassin';
    /**
		Recupererle point de la muraille par son id
     */
    public function getPointbassin($idpointbassin) {
        $idpointbassin = (int)$idpointbassin;
        $row = $this->fetchRow('idpointbassin = ' . $idpointbassin);
        if (!$row) {
            throw new Exception("Count not find row $idpointbassin");
        }
        return $row->toArray();    
    }
    
	public function getBassin($idbassin) {
		$idbassin = (int)$idbassin;
		$row = $this->fetchAll('bassinderetention_idbassinderetention = ' . $idbassin);
		return $row->toArray();    
	}

    public function addPointbassin($latitude, $longitude, $bassin, $pointbassin_idpointbassin_pred) {
        $data = array(
            'latitude' => $latitude,
            'longitude' => $longitude,
            'bassinderetention_idbassinderetention' => $bassin,
            'pointbassin_idpointbassin_pred' => $pointbassin_idpointbassin_pred,
        );
        $this->insert($data);
    }
    
    public function addij($i, $j) {
        $data = array(
            'latitude' => "",
            'longitude' => "",
            'bassinderetention_idbassinderetention' => "",
            'pointbassin_idpointbassin_pred' => "",
        	'i' => $i,
        	'j' => $j,
        );
        $this->insert($data);
    }
    public function updatePointbassin($idpointbassin, $latitude, $longitude, $bassin, $pointbassin_idpointbassin_pred) {
         $data = array(
            'latitude' => $latitude,
            'longitude' => $longitude,
            'bassinderetention_idbassinderetention' => $bassin,
            'pointbassin_idpointbassin_pred' => $pointbassin_idpointbassin_pred,
        );
        $this->update($data, 'idpointbassin = '. (int)$idpointbassin);
    }
    
	function deletePointbassin($idpointbassin) {
		$this->delete ( 'idpointbassin =' . ( int ) $idpointbassin );
	}
    /**
     * 
     * @return  
     */
	public function getAllPointbassin() {
		$all = $this->fetchAll('pointbassin_idpointbassin_pred=0');
		$pointbassins = array();
		$pointbassins['total'] = count($all->toArray());
		$pointbassins['rows'] = $all->toArray();
		$bassin = new Admin_Model_DbTable_Bassinderetention();
		$pointbassins['info'] = array();
		for($i=0;$i<count($all->toArray());$i++) {
			$pointbassins['info'][$i] = 
			$bassin->getBassin($pointbassins['rows'][$i]['bassinderetention_idbassinderetention']);
			$pointbassins['rows'][$i] = 
			$this->getBassin($pointbassins['rows'][$i]['bassinderetention_idbassinderetention']);
		}
		return $pointbassins ;
	}    
	
public function getCoordonneee($imin,$imax,$jmin,$jmax) {    
 		$select = $this ->select()
             -> from('pointbassin')
             -> where('i > ?', $imin)
             -> where('i <= ?', $imax)
             -> where('j > ?', $jmin)
             -> where('j <= ?', $jmax);

        $result = $this -> fetchAll($select);
        $res = array();
        $i = 0;
     	foreach ($result as $row) {
     		$res[$i] = array();
 			$res[$i] = $row->toArray();
 			$res[$i]["type"] = "pointbassin";
 			$i++;

		}
        return $res;
    }
}
